package com.yl.rabbitmq.day04;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.yl.rabbitmq.utils.RabbitMQUtil;
import com.yl.rabbitmq.utils.SleepUtil;

/**
 * @author yangl
 * 手动应答，设置不公平分发，预取值
 */
public class Consumer0401 {

    public static final String TASK_QUEUE_NAME = "durable_ack_name";

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitMQUtil.getChannel();
        System.out.println("C1等待接收消息的处理时间较短");
        //声明接收消息
        DeliverCallback deliverCallback = (consumerTag, message)->{
            SleepUtil.sleep(1);
            System.out.println("接收到的消息：" + new String(message.getBody()));
            /**
             * 手动应答
             * 1、消息的标识
             * 2、是否批量应答， true：批量，false：不批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        //声明取消接收消息
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消息被消费者取消消费接口回调逻辑");
        };
        //设置不公平分发
        //默认是1，如果设置是其他值，就是预取值了。
        channel.basicQos(2);
        channel.basicConsume(TASK_QUEUE_NAME, false,deliverCallback,cancelCallback);
    }
}
